<template>
  <div class="login-wrapper">
    <!-- 外层盒子 -->
    <div class="login-box">
      <!-- 头像区域 -->
      <div class="avatar-wrapper">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- 登录表单区域 -->
      <el-form ref="loginFormRef" class="login-form" :model="loginForm" :rules="loginFormRule">
        <el-form-item prop="username">
          <!-- 用户区域 -->
          <el-input v-model="loginForm.username" prefix-icon="iconfont icon-user"></el-input>
        </el-form-item>
        <!-- 密码区域 -->
        <el-form-item prop="password">
          <el-input type="password" v-model="loginForm.password" prefix-icon="iconfont icon-3702mima"></el-input>
        </el-form-item>
        <!-- 按钮区域 -->
        <el-form-item class="btn-wrapper">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 登录绑定数据
      loginForm: {
        username: "admin",
        password: "123456"
      },
      // 定义表单的验证规则
      loginFormRule: {
        // 验证用户名
        username: [
          { required: true, message: "用户名不能为空", trigger: "blur" },
          { min: 2, max: 6, message: "用户名应该在2到6个字符之间", trigger: "blur" }
        ],
        // 验证密码
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" },
          { min: 6, max: 16, message: "密码应该在6到16个字符之间", trigger: "blur" }
        ]
      }
    }
  },
  methods: {
    // 重置表单
    resetForm() {
      // resetFields:重置表单
      this.$refs.loginFormRef.resetFields()
    },
    // 登录
    login() {
      // 校验成功后登录
      this.$refs.loginFormRef.validate(async valid => {
        if(!valid) return;
        let res = await this.axios.post('/login', this.loginForm)
        let { data, meta } = res.data;
        if(meta.status !== 200) {
          this.$message.error('登录失败');
        } else {
          this.$message.success('登录成功'); 
          sessionStorage.token = data.token;
          //导航跳转页面
          this.$router.push('/home')
        }
      })
    }
  }
};
</script>

<style lang="scss" scoped>
/* 最外层盒子样式 */
.login-wrapper {
  width: 100%;
  height: 100%;
  background: #2b4b6b;
}
/* 登录表单盒子样式 */
.login-box {
  width: 450px;
  height: 304px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: #fff;
  border-radius: 3px;
  box-shadow: 0 0 10px #ddd;
}
/* 头像样式 */
.avatar-wrapper {
  padding: 10px;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 130px;
  height: 130px;
  background: #fff;
  border: 1px solid #efefef;
  border-radius: 50%;
  box-shadow: 0 0 10px #ddd;
  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #efefef;
  }
}
/* 表单样式 */
.login-form {
  padding: 0 20px;
  width: 100%;
  position: absolute;
  bottom: 0;
  box-sizing: border -box;
  .btn-wrapper {
    display: flex;
    justify-content: flex-end;
  }
}
</style>